package com.xbh.Book.filter;

import com.xbh.Book.utils.JDBCUtils;

import javax.servlet.*;
import java.io.IOException;

/**
 * @program: JavaWebExer
 * @description: 统一的给所有的Servlet程序加上try-catch处理
 * @author: 许宝华
 * @create: 2021-05-04 16:00
 */

public class TransactionFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            chain.doFilter(request,response);
            JDBCUtils.commitAndClose();//提交事务
        } catch (Exception e) {
            JDBCUtils.rollbackAndClose();//回滚事务
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
